package com.sakura.mall.product.commentReplay.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.commentReplay.domain.entity.CommentReplayEntity;
import com.sakura.mall.product.commentReplay.domain.request.AddCommentReplayRequest;
import com.sakura.mall.product.commentReplay.domain.request.UpdateCommentReplayRequest;
import com.sakura.mall.product.commentReplay.domain.request.CommentReplayPageRequest;
import com.sakura.mall.product.commentReplay.domain.vo.CommentReplayVo;
import com.sakura.mall.product.commentReplay.mapper.CommentReplayMapper;
import com.sakura.mall.product.commentReplay.service.CommentReplayService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品评价回复关系 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CommentReplayServiceImpl extends ServiceImpl<CommentReplayMapper, CommentReplayEntity> implements CommentReplayService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCommentReplayRequest request) {
        // todo 其他校验逻辑
        CommentReplayEntity commentReplay = request.convert(CommentReplayEntity.class);
        this.save(commentReplay);
    }

    @Override
    public Page<CommentReplayVo> queryPage(CommentReplayPageRequest request) {
        IPage<CommentReplayEntity> qp = request.toPage();
        LambdaQueryWrapper<CommentReplayEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CommentReplayVo> ret = qp.convert(entity -> entity.convert(CommentReplayVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCommentReplayRequest request) {
        CommentReplayEntity entity = request.convert(CommentReplayEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

